<?php
$adm_menu = "1";

include("config.php");
if (!isset($_SESSION["auser"]) || $_SESSION["auser"] == "" || $_SESSION['agroup'] != "WEB_ADMIN") header('Location: index.php');


$db_www = new DB($server_username,$server_pass,$server_db,$server,$driver);
$db_rdata = new DB($rdata_server_username,$rdata_server_pass,$rdata_server_db,$rdata_server,$rdata_driver);

if (isset($_REQUEST["act"])){
	if ($_REQUEST["act"] == "save") {	
	
		$code = $_REQUEST["code"];
		$sql = str_replace("<kodas>", addslashes_mssql($code), $uinfo_query);
		$u = $db_www->returnInfo($sql);
		if ($u == null) die("nerastas vartotojas");
		$imcode = trim($u["n60_kodas_ks"]);	
	
		$sql = "exec web_update_user_nuol "
		. "@N60_KODAS_SM='".addslashes_mssql($imcode)."',"
		. "@N08_kodas_ls_6='".addslashes_mssql($_REQUEST["disctype"])."'";		
		$db_www->execute($sql);
		if (($_REQUEST["disctype"] == "PROTOKOLAS" || $_REQUEST["disctype"] == "PROTOKOLAS_S") && isset($_REQUEST["antk"]) && is_numeric($_REQUEST["antk"])&& isset($_REQUEST["antk2"]) && is_numeric($_REQUEST["antk2"])) {
			$sql = "exec web_ins_upd_protocol "
			. "@N08_c_kodas_ks='".addslashes_mssql($imcode)."',"
			. "@N08_c_protokolas='".addslashes_mssql($_REQUEST["antk"])."',"
			. "@N08_c_protokolas_p='".addslashes_mssql($_REQUEST["antk2"])."'";
			$db_rdata->execute($sql);		   
		}
		header('Location: user.php?code='.$_REQUEST["code"]);
	}
}
	
include("header.php");
include("menu.php");

$antk ='';

	if (isset($_REQUEST["code"]) && $_REQUEST["code"] != "0") {
		$code = $_REQUEST["code"];
		$uid = $code;		
		if (isset($_REQUEST["uid"])) {
			$sql = str_replace("<kodas>", addslashes_mssql($_REQUEST["uid"]), $uinfo_query);
		} else {
			$sql = str_replace("<kodas>", addslashes_mssql($uid), $uinfo_query);
		}
		
		$u = $db_www->returnInfo($sql);
		if ($u == null) die("nerastas vartotojas");
		$imcode = trim($u["n60_kodas_ks"]);	
		$test_q = "SELECT N08_kodas_ls_6 FROM n08_klij WHERE N08_kodas_ks = '".$imcode."'";
		
		$im = $db_www->returnInfo($test_q);		
		if (!empty($im)) {
			$nuol_sist_tipas = trim($im["N08_kodas_ls_6"]);
		}else {
		    $nuol_sist_tipas = "";
	    }
		if ($nuol_sist_tipas=='PROTOKOLAS' || $nuol_sist_tipas=='PROTOKOLAS_S'){
			$test_q = "SELECT N08_c_protokolas,N08_c_protokolas_n FROM n08_klij_c WHERE N08_c_kodas_ks = '".$imcode."'";
			
			$imm = $db_rdata->returnInfo($test_q);		
			if (!empty($imm)) {
				$nuol_dydis = trim($imm["N08_c_protokolas"]);
				$nuol_dydis2 = trim($imm["N08_c_protokolas_n"]);
			}else {
				$nuol_dydis = "";
				$nuol_dydis2 = "";
			}		  
		}else {
		    $nuol_dydis = "";
			$nuol_dydis2 = "";
	    }
	} else {
		die("nerastas vartotojas");
	}

		
		$tit = "Nuolaidos";		
?>
<form method="post" name='forma' action='adiscounts.php?act=save'>
<table class="innercont" cellspacing="0" cellpadding="10" border="0">
<tr><td class="leftcol2">


<?php
printUserEditMenu($_SESSION["page_ret"],$_REQUEST["code"],$u["n60_kodas_sm"],$u["n60_pareigos"]);

if ($nuol_sist_tipas == 'PROTOKOLAS' || $nuol_sist_tipas == 'PROTOKOLAS_S'){
   $protshow ='';
} else {
   $protshow ='style="display:none;"';
}
?>


</td><td class="rightcol" colspan="2">

<div class="title2" style="width:400px;"><?php echo $tit?></div>

<input id="code" type="hidden" name="code" value="<?php echo $code?>">
<table width="100%" border="0">
<tr><td valign="top" >
<table border="0" style='width:400px;'>
<tr><td valign="top" class="fieldtitle">Nuolaidų sistemos tipas:</td>
<td valign="top" class="fieldtitle"><span id='antkpav' <?php echo $protshow;?>>Protokolo<br>antkainis:</span></td>
<td valign="top" class="fieldtitle"><span id='antkpav2' <?php echo $protshow;?>>Protokolo antkainis partnerių deg.:</span></td>
</tr>
<tr>
<td valign="top">
<script  type="text/javascript">
	$(document).ready(function(){	

		if (!$.browser.opera) {
    
			// select element styling
			$('select.select2').each(function(){
				var title = $(this).attr('title');
				if( $('option:selected', this).val() != ''  ) title = $('option:selected',this).text();
				$(this)
					.css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
					.after('<span class="select2">' + title + '</span>')
					.change(function(){
						val = $('option:selected',this).text();
						$(this).next().text(val);
						})
			});

		};
		
	});
	function showfields(){
		
		
		disctype = document.getElementById("disctype");
		antk = document.getElementById("antk");
		antkpav= document.getElementById("antkpav");
		antk2 = document.getElementById("antk2");
		antkpav2= document.getElementById("antkpav2");		
		if (disctype.value.trim() == 'PROTOKOLAS' || disctype.value.trim() == 'PROTOKOLAS_S') {			
			antk.style.display = "inline";
			antkpav.style.display = "inline";
			antk2.style.display = "inline";
			antkpav2.style.display = "inline";			
		} else  {
			antkpav.style.display = "none";
			antk.style.display = "none";
			antkpav2.style.display = "none";
			antk2.style.display = "none";			
		} 
	}
function isNumeric(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}
	
function validateForm(myform)
{
    disctype = document.getElementById("disctype");
	antk = document.getElementById("antk");	
	antk2 = document.getElementById("antk2");	
	if (disctype.value.trim() == 'PROTOKOLAS' || disctype.value.trim() == 'PROTOKOLAS_S') {
	 if (!isNumeric(antk.value)){
	    alert("Neteisingas antkainis");
		return;	
	 }
	 if (!isNumeric(antk2.value)){
	    alert("Neteisingas partnerių antkainis");
		return;	
	 }	 
	}
	myform.submit();
}	
</script>
<div style="position: relative;">
<select name="disctype" class="select2" id='disctype' onchange='showfields();'>
<?php
$sql = "select n35_kodas_ls,n35_pav from n35_lgst where n35_tevas = 'KLIENT_NUOL'";
$list = $db_www->returnList($sql);
	foreach ($list as $item) {
		$lid = @iconv('windows-1257', 'UTF-8', trim($item["n35_kodas_ls"]));
		$lpav = @iconv('windows-1257', 'UTF-8', trim($item["n35_pav"]));
		if ($nuol_sist_tipas == trim($item["n35_kodas_ls"])){
		   $sel ="selected";
		} else {
		   $sel = "";
		}
		echo '<option value="'.$lid.'" '.$sel.'>'.htmlspecialchars($lpav).'</option>';
	}
?>

</select>
</div>
</td>
<td><input <?php echo $protshow;?> id="antk" autocomplete="off" class="inp" type='text' name="antk" value="<?php echo htmlspecialchars(sprintf("%01.3f",$nuol_dydis));?>"></td>
<td><input <?php echo $protshow;?> id="antk2" autocomplete="off" class="inp" type='text' name="antk2" value="<?php echo htmlspecialchars(sprintf("%01.3f",$nuol_dydis2));?>"></td>
</tr>
</table>
</td></tr>

<tr><td>
<table cellpading="0" cellspacing="0" width="400">
<tr><td valign="top" class="fieldtitle">Nuolaidos prekių grupėms:</td><td align="right"><a class="flink" href="edit_discounts.php?code=<?php echo $code;?>">Koreguoti lentelę</a></td></tr>
</table>
</td></tr>
<tr><td valign="top">
<table cellspacing="0" cellpadding="5" align="left" class="innercont3">
<tr>
<th>Prekių grupė</th><th>Grupės pavadinimas</th><th>Nuolaidos tipas</th><th>Nuolaidos dydis</th></tr>
<?php
$sql = "SELECT n.* FROM n08_klij_n n WHERE n.N08_n_kodas_ks='".addslashes_mssql($code)."'";

$list = $db_rdata->returnList($sql);
foreach ($list as $item) {
	echo "<tr>";
	echo "<td>".htmlspecialchars($item["N08_n_kodas_gs"])."</td>";
	echo "<td>".htmlspecialchars(getNuolGrPav($item["N08_n_kodas_gs"]))."</td>";
	echo "<td>";
	if ($item["N08_n_tipas"] == 1) {
		echo "Suma vienetui";
	} else {
		echo "Procentai";
	}
	
	echo "</td>";
	echo "<td>".htmlspecialchars($item["N08_n_dydis"])."</td>";
	echo "</tr>";
}

?>

</table>
</td></tr>
</table>



</td></tr>

<tr><td>&nbsp;</td><td>

<Button id='unblockButton' class='but bgwhite nopad' onclick="window.location='<?php if ($_SESSION["page_ret"] == "users") echo "users.php"; else echo "acards.php?submit=Rodyti";?>';return false;">Atgal</Button>
&nbsp;&nbsp;&nbsp;&nbsp;<input class="but bgwhite nopad" type="submit" value="Išsaugoti" onclick="validateForm('forma');return false;">
<?php

?>
</td>
<td style="text-align:right;">

</td>
</tr>
</table>
</form>
<?php
include("footer.php");
?>